import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
export default{
	props:{
		id:String
	},
	components:{ElImageViewer},
	data(){
		return{
			detailData:{},
			showDialog:false,
			formData:{
				userName:JSON.parse(localStorage.user).user.name,
				describe:"",
				multipartFile:[],
			},
			activeOpt:"",
			approveStateList:[],
			apiFun:"infrastructure_qiaoliangDetail",
			btnLoading:false,
			showViewer:false,
			imgList:[],
		}
	},
	watch:{
		id(){
			this.getDetailData()
		}
	},
	methods:{
		getDetailData(){
			if(!this.id){
				return
			}
			this.$http[this.apiFun]({id:this.id})
			.then((res)=>{
				var data=res.data.data;
				data.picUrl=data.picUrl.split(",");
				this.approveStateList.forEach((item)=>{
					if(item.code==data.status){
						data.statusName=item.name
					}
					data.approvePatrol.forEach((ap)=>{
						if(item.code==ap.status){
							ap.statusName=item.name
						}
						if(ap.status==2 && data.status==3 || ap.status==2 && data.status==4){
							ap.statusName="已处理"
						}else if(ap.status==3 && data.status==4){
							ap.statusName="已复核"
						}
					})
				})
				data.approvePatrol.forEach((ap)=>{
					if(ap.checkpic){
						ap.checkpic=ap.checkpic.split(",");
					}else{
						ap.checkpic=[]
					}
				})
				this.detailData=data;
			}).catch((err)=>{
				throw(err)
			})
		},
		
		//审批状态
		getApprovStatus(){
			this.$http.getDictKeyList({parentCode:'patrolStatus'})
			.then((res)=>{
				this.approveStateList=res.data.data ||[]
			}).catch((err)=>{
				throw(err)
			})
		},
		
		
		onProgress(event, file, fileList){
			this.formData.multipartFile=fileList
		},
		
		onChange(file, fileList){
			this.formData.multipartFile=fileList
		},
		
		approve(btn){
			this.activeOpt=btn;
			this.formData.id=this.id;
			this.formData.type=this.type
			if(btn=='tongguo'){
				this.btnLoading=true
				this.$http.approvePatrol_pass(this.formData)
				.then((res)=>{
					this.getDetailData()
					this.$emit("refresh",1)
					this.btnLoading=false
				}).catch((err)=>{
					this.btnLoading=false
					throw(err)
				})
			}else if(btn=='quchuli'){
				this.btnLoading=true
				this.$http.approvePatrol_toProcess(this.formData)
				.then((res)=>{
					this.getDetailData()
					this.$emit("refresh",1)
					this.btnLoading=false
				}).catch((err)=>{
					this.btnLoading=false
					throw(err)
				})
			}else if(btn=='chuliwancheng'){
				this.showDialog=true;
				this.btnLoading=false
			}else if(btn=='fuhewancheng'){
				this.showDialog=true;
				this.btnLoading=false
			}
		},
		
		onCancel(){
			this.formData.describe=""
			this.formData.userName=""
			this.formData.multipartFile=[]
			this.showDialog=false
		},
		
		onSubmit(){
			if(this.formData.describe==""){
				this.$message.warning("请输入审批意见!")
				return
			}else if(this.formData.multipartFile.length==0){
				this.$message.warning("请上传附件!")
				return
			}
			var formdata=new FormData;
			formdata.append("id",this.id)
			formdata.append("type",this.type)
			formdata.append("userName",this.formData.userName)
			formdata.append("describe",this.formData.describe)
			this.formData.multipartFile.forEach((file,index)=>{
				formdata.append("multipartFile"+index,file.raw)
			})
			this.btnLoading=true
			if(this.activeOpt=='chuliwancheng'){
				this.$http.approvePatrol_completion(formdata)
				.then((res)=>{
					this.getDetailData()
					this.$emit("refresh",1)
					this.formData.describe=""
					this.formData.userName=""
					this.formData.multipartFile=[]
					this.showDialog=false;
					this.btnLoading=false
				}).catch((err)=>{
					this.btnLoading=false
					throw(err)
				})
			}else if(this.activeOpt=='fuhewancheng'){
				this.$http.approvePatrol_reApprove(formdata)
				.then((res)=>{
					this.getDetailData()
					this.$emit("refresh",1)
					this.formData.describe=""
					this.formData.userName=""
					this.formData.multipartFile=[]
					this.showDialog=false;
					this.btnLoading=false
				}).catch((err)=>{
					this.btnLoading=false
					throw(err)
				})
			}
		},
		
		alertMsg(){
		    this.$message.warning("上传图片不超过9张！")
		},
		
		showImageViewer(item){
			this.showViewer=true;
			this.imgList=item.checkpic;
		},
		closeViewr(){
			this.showViewer=false
		},
		
		
	},
	created(){
		this.getApprovStatus()
	}
}